package com.huilan.etl.util;

import com.taocares.sdxp.SdxpConstants;
import com.taocares.sdxp.client.SDXPClient;
import com.taocares.sdxp.client.SDXPClientFactory;
import com.taocares.sdxp.model.LoginResult;
import com.taocares.sdxp.model.MessageResult;
import com.taocares.sdxp.model.SendResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 消息工具类
 *
 * @author ghy
 * @date 2019-01-03 16:37
 **/
@Slf4j
@Component
@PropertySource("classpath:config.properties")
public class MessageUtil {

    private static String host;

    @Value("${sdxp.host}")
    public void setHost(String host) {
        MessageUtil.host = host;
    }

    private static Integer port;
    @Value("${sdxp.port}")
    public void setPort(Integer port) {
        MessageUtil.port = port;
    }

    private static String username;
    @Value("${sdxp.username}")
    public void setUsername(String username) {
        MessageUtil.username = username;
    }

    private static String password;
    @Value("${sdxp.password}")
    public void setPassword(String password) {
        MessageUtil.password = password;
    }

    private  static SDXPClient sdxpClient = SDXPClientFactory.createWSClient(host, port);


    private static void sendMessage(List<String> messageList){

        SendResult sendResult = sdxpClient.sendMessage(messageList);

        if(!sendResult.isSuccess()&&sendResult.getCode()== SdxpConstants.RC_CLIENT_NOT_LOGGED_IN||sendResult.getCode()==SdxpConstants.RC_TOKEN_EXPIRED){
            if(login()){
                sendResult = sdxpClient.sendMessage(messageList);
            }
        }

        if(sendResult.isSuccess()){
            log.info("用户：{}成功发送消息{}条",username,messageList.size());
        }else{
            log.error("用户：{}发送消息失败",username,sendResult.getMessage());
        }
    }

    private static boolean login(){
        LoginResult loginResult = sdxpClient.login(username,password);
        if(loginResult.isSuccess()){
            log.info("用户：{}登录平台成功",username);
        }else{
            log.error("用户：{}登录失败,{}",username,loginResult.getMessage());
        }
        return loginResult.isSuccess();
    }


    public static List<String> receiveMessage(){
        MessageResult messageResult = sdxpClient.receiveMessage();
        if(!messageResult.isSuccess()&&messageResult.getCode()==SdxpConstants.RC_CLIENT_NOT_LOGGED_IN||messageResult.getCode()==SdxpConstants.RC_TOKEN_EXPIRED){
            if(login()){
                messageResult = sdxpClient.receiveMessage();
            }
        }
        if(messageResult.isSuccess()){
            log.info("用户：{}成功接收消息{}条",username,messageResult.getMessageStringList().size());
        }else{
            log.error("用户：{}接收消息失败，{}",username,messageResult.getGuid());
        }
        return messageResult.getMessageStringList();
    }
}
